﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;

namespace BLL.Pages
{
    public class Pages:BasePages
    {
        #region "Constructors"
        public Pages() { }

        public Pages(int id, DateTime addedDate, string addedBy,string title, int importance, string Abstract, string body, int option)
        {
            this.ID = id;
            this.AddedDate = AddedDate;
            this.AddedBy = addedBy;
            this.Title = title;
            this.Importance = importance;
            this.Abstract = Abstract;
            this.Body = body;
            this.Option = option;
        }
        #endregion

        #region "Public Properties"

        public string Title { get; set; }

        public int Importance { get; set; }

        public string Abstract { get; set; }

        public string Body { get; set; }

        public int Option { get; set; }

        public bool checkimage
        {
            get 
            {
                if (this.AddedBy.Trim() == "1")
                    return true;
                else
                    return false;
            }   
        }

        public bool checkflash
        {
            get
            {
                if (this.AddedBy.Trim() == "2")
                    return true;
                else
                    return false;
            }
        }

        #endregion

        #region Get Page From PagesDetail
        private static Pages GetPageFromPagesDetail(PagesDetails record)
        {
            if (record == null)
                return null;
            else
                return new Pages(record.PageID, record.AddedDate, record.AddedBy, record.Title, record.Importance, record.Abstract, record.Body, record.Option);
        }

        private static List<Pages> GetPageFromListPageDetail(List<PagesDetails> recordset)
        {
            List<Pages> Pages = new List<Pages>();
            foreach (PagesDetails record in recordset)
                if(record.PageID!=4)
                    Pages.Add(GetPageFromPagesDetail(record));
            return Pages;
        }
        #endregion

        #region Method
        public static Pages GetPage(int PageID)
        {
            Pages Page = null;
            string key = "Page_" + PageID.ToString();
            if (BasePages.Settings.EnableCaching && BizObject.Cache[key] != null)
                Page = (Pages)BizObject.Cache[key];
            else
            {
                Page = GetPageFromPagesDetail(SiteProvider.Pages.PageGet(PageID));
                BasePages.CacheData(key, Page);
            }
            return Page;
        }

        public static List<Pages> ListPage()
        {
            List<Pages> Pages = null;
            string key = "Pages";
            if (BasePages.Settings.EnableCaching && BizObject.Cache[key] != null)
                Pages = (List<Pages>)BizObject.Cache[key];
            else
            {
                List<PagesDetails> recordset = SiteProvider.Pages.ListPage();
                Pages = GetPageFromListPageDetail(recordset);
                BasePages.CacheData(key, Pages);
            }
            return Pages;
        }

        public static int InsertPage(string title, int importance, string Abstract, string body)
        {
            if (importance == null)
                importance = 0;
            if (title == null)
                title = "";
            if (Abstract == null)
                Abstract = "";
            if (body == null)
                body = "";
            Abstract = Helpers.ConvertNullToEmptyString(Abstract);
            body = Helpers.ConvertNullToEmptyString(body);
            PagesDetails record = new PagesDetails(0, DateTime.Now,BizObject.CurrentUserName,title,importance,Abstract,body,0);
            int ret = SiteProvider.Pages.InsertPage(record);
            BizObject.PurgeCacheItems("Page");
            return ret;
        }

        public static bool UpdatePage(int id, string title, int importance, string Abstract, string body)
        {
            if (importance == null)
                importance = 0;
            if (title == null)
                title = "";
            if (Abstract == null)
                Abstract = "";
            if (body == null)
                body = "";
            Abstract = Helpers.ConvertNullToEmptyString(Abstract);
            body = Helpers.ConvertNullToEmptyString(body);
            PagesDetails record = new PagesDetails(id, DateTime.Now, BizObject.CurrentUserName, title, importance, Abstract, body, 0);
            bool ret = SiteProvider.Pages.UpdatePage(record);
            BizObject.PurgeCacheItems("Page");
            return ret;
        }

        public static bool UpdatePageOption(int id, string title, int importance, string Abstract, string body, int option)
        {
            if (importance == null)
                importance = 0;
            if (title == null)
                title = "";
            if (Abstract == null)
                Abstract = "";
            if (body == null)
                body = "";
            Abstract = Helpers.ConvertNullToEmptyString(Abstract);
            body = Helpers.ConvertNullToEmptyString(body);
            PagesDetails record = new PagesDetails(id, DateTime.Now, BizObject.CurrentUserName, title, importance, Abstract, body,option);
            bool ret = SiteProvider.Pages.UpdatePageOption(record);
            BizObject.PurgeCacheItems("Page");
            return ret;
        }

        public static bool DeletePage(int id)
        {
            bool ret = SiteProvider.Pages.DeletePage(id);
            BizObject.PurgeCacheItems("Page");
            return ret;
        }
        #endregion
    }
}
